package com.gzsxy.esjy.common.base.util;


import javax.servlet.http.HttpServletRequest;

/**
 * @author xiaolong
 * @version 1.0
 * @description: request请求工具类
 * @date 2021/11/16 17:01
 */
public class RequestUtil {


    /**
     * @description: 从HTTP请求中获取客户端IP地址
     * @param request
     * @return  String 客户端IP地址
     */
    public static String getClientIp(HttpServletRequest request){
        String ip = request.getHeader("X-Real-IP");
        //注意本地测试时，浏览器请求不要用localhost，要用本机IP访问项目地址，不然这里取不到ip

        //负载均衡
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("x-forwarded-for");
        }
        // 是在Weblogic下获取真实IP所用的的参数
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        //代理客户端的IP
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
